package cn.yanxiaohui.service.web.controller;

import cn.yanxiaohui.common.exception.APIException;
import cn.yanxiaohui.common.util.constant.RequestCode;
import cn.yanxiaohui.common.vo.ActionResult;
import cn.yanxiaohui.data.entity.User;
import cn.yanxiaohui.service.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制器
 * @author yanxh
 * 
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
	/** 视图统一前缀 **/
	private final static String PREFIX = "user/";

	@Autowired
	UserService userService;


	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(ModelMap model) {
		Map<String,Object> params = new HashMap<String,Object>();
		List<User> userList = userService.listByParams(params);
		model.addAttribute("userList",userList);
		return PREFIX + "index";
	}



	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public ActionResult insert(ModelMap model, User user) {
		try{
			userService.insertUser(user);
			return ActionResult.success();
		}catch (APIException e){
			return ActionResult.error(e.getErrorCode(),e.getMessage());
		}catch (Exception e){
			e.printStackTrace();
			return ActionResult.error(RequestCode.ERROR,e.getMessage());
		}
	}
}
